'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/auto_run/pub/compute_copyright_data 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			item,
			todayStart,
			todayEnd,
			last_StartTime,
			last_EndTime,
			yesterday
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		try {
			var array_copyright = item.array_copyright;
			console.log("arry_copyright", array_copyright);
			var array = [];
			for (let i of array_copyright) {
				for (let y of i.tvName) {
					array.push(y);
				}
			}
			console.log("array=》", array);
			for (let i of array) {
				//查询昨该剧充值总金额
				const rechargeNumber = await vk.baseDao.sum({
					dbName: "a_recharge",
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						tv_name: i,
						pay_status: 1,
						_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
					}
				});

				if (rechargeNumber > 0) {
					let commission = 0;
					for (let y of array_copyright) {
						if (y.tvName.includes(i)) {
							console.log();
							commission = y.commission_rate * rechargeNumber * 1;
						}
					}
					console.log("");
					let id = await vk.baseDao.add({
						dbName: "b_copyrightData", // 表名
						dataJson: { // 需要新增的数据 json格式
							"bank": item.bank || 0,
							"paymentAccount": item.paymentAccount || 0,
							"paymentCompany": item.paymentCompany || 0,
							"copyrightId": item._id,
							"copyrightUsername": item.username,
							"copyrightNickname": item.nickname,
							"tvName": i,
							"rechargeNumber": rechargeNumber,
							"commission": commission,
							"isPayment": false,
							"_add_time": last_StartTime,
							"_add_time_str": vk.pubfn.timeFormat(last_StartTime, "yyyy-MM-dd")
						}
					});
				}
			}
			await updateCopyrights();
		} catch (err) {

		}





		// 业务逻辑结束-----------------------------------------------------------
		return res;
		async function updateCopyrights() {
			let updateCopyrights = await vk.baseDao.update({
				dbName: "uni-id-users", // 表名
				whereJson: { // 条件
					_id: item._id, // true：存在 false：不存在
				},
				dataJson: { // 需要修改的数据
					isComputed: 1, //当天是否已经计算，0没有计算，1已经计算
				}
			});
			console.log("updateCopyrights=", updateCopyrights);
		}
	}
}